﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using System.Data;
using System.Text;
using Maticsoft.DBUtility;
using Utils;
using System.Collections.Specialized;
using Maticsoft.Model;


public partial class UserControls_tuanmore : FBasePage 
{
    // Fields
    public BaseUserControl baseuser = new BaseUserControl();


    protected Team current = null;
    protected Team CurrentTeam = null;
    protected long curtimefix = 0L;
    private string detail = "";
    protected long difftimefix = 0L;

    protected int imageindex = 2;
    protected int offset = 0;
    protected NameValueCollection order = new NameValueCollection();
    public Maticsoft.BLL.Order orderbll = new Maticsoft.BLL.Order();
    public List<Order> orderlist = new List<Order>();
    protected List<Team> otherteams = new List<Team>();
    protected bool over = false;
    protected DateTime overtime = DateTime.Now;
    protected Partner part = new Partner();
    protected NameValueCollection partner = new NameValueCollection();
    protected int size = 0;
    public int sortorder = 0;
    protected TeamState state = TeamState.none;
    public string strtitle = "";
    protected NameValueCollection team = new NameValueCollection();
    protected int teamid = 0;
    protected string teamstatestring = string.Empty;
    protected int twoline = 0;
    protected NameValueCollection user = new NameValueCollection();
    public Maticsoft.BLL.UserInfo userbll = new Maticsoft.BLL.UserInfo();
    public UserInfo usermodel = new UserInfo();

    // Methods
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        this.teamid = Helper.GetInt(base.Request["id"], 0);
        if (this.teamid > 0)
        {
            this.CurrentTeam = new Maticsoft.BLL.Team().GetModel(this.teamid);
        }
        else
        {
            this.CurrentTeam = base.CurrentTeam;
        }
        if (this.CurrentTeam != null)
        {
            if ((this.CurrentTeam != null) && (base.ASSystem.Description == string.Empty))
            {
                this.Page.Header.Controls.Add(new LiteralControl("<meta name=\"description\" content=\"" + this.CurrentTeam.Summary.Replace("\r\n", "") + "\" />\r\n"));
            }
            this.orderlist = this.orderbll.GetByUserid(this.userbll.Getuserid(base.UserName), this.CurrentTeam.Id);
            if (this.CurrentTeam.Buyonce == "Y")
            {
                if ((this.CurrentTeam.Now_number < this.CurrentTeam.Max_number) && (this.orderlist.Count > 0))
                {
                    this.SetError("您已经成功购买了本单产品，请勿重复购买，快去关注一下其他产品吧！");
                }
            }
            else if (((this.CurrentTeam.Buyonce == "N") && (this.CurrentTeam.Now_number < this.CurrentTeam.Max_number)) && ((this.orderlist.Count != 0) && (this.orderlist.Count >= this.CurrentTeam.Per_number)))
            {
                this.SetError("您购买本单产品的数量已经达到上限，快去关注一下其他产品吧！");
            }
        }
        if (this.CurrentTeam == null)
        {
            base.Response.End();
        }
        else
        {
            this.twoline = (base.ASSystem.teamwhole == 0) ? 1 : 0;
            Maticsoft.BLL.Partner partner = new Maticsoft.BLL.Partner();
            this.curtimefix = Helper.GetTimeFix(DateTime.Now) * 0x3e8L;
            this.difftimefix = Helper.GetTimeFix(this.CurrentTeam.End_time) * 0x3e8L;
            this.difftimefix -= this.curtimefix;
            this.part = partner.GetModel(this.CurrentTeam.Partner_id);
            this.partner = Helper.GetObjectProtery(this.part);
            this.state = base.GetState(this.CurrentTeam);
            if ((this.state == TeamState.fail) || (this.state == TeamState.successtimeover))
            {
                this.over = true;
                if (this.CurrentTeam.Close_time.HasValue)
                {
                    this.overtime = this.CurrentTeam.Close_time.Value;
                }
                else
                {
                    this.overtime = this.CurrentTeam.End_time;
                }
            }
            if (this.state == TeamState.fail)
            {
                this.teamstatestring = "failure";
            }
            else if ((this.state == TeamState.successbuy) || (this.state == TeamState.successtimeover))
            {
                this.teamstatestring = "success";
            }
            else if (this.state == TeamState.successnobuy)
            {
                this.teamstatestring = "soldout";
            }
            if (this.CurrentTeam.Team_type == "seconds")
            {
                this.strtitle = "今日秒杀";
            }
            if (this.CurrentTeam.Team_type == "goods")
            {
                this.strtitle = "今日热销";
            }
            if (this.CurrentTeam.Team_type == "normal")
            {
                this.strtitle = "今日团购";
            }
            if (base.AsUser != null)
            {
                List<Order> modelList = new Maticsoft.BLL.Order().GetModelList(string.Concat(new object[] { "Team_id=", this.CurrentTeam.Id, " and User_id=", base.AsUser.Id, " and State='unpay'" }));
                if (modelList.Count > 0)
                {
                    this.order = Helper.GetObjectProtery(modelList[0]);
                }
            }
            this.team = Helper.GetObjectProtery(this.CurrentTeam);
            if (this.CurrentTeam.Min_number > 0)
            {
                this.size = Convert.ToInt32(Math.Ceiling(Convert.ToDouble((double)(190.0 * (Convert.ToDouble(this.CurrentTeam.Now_number) / Convert.ToDouble(this.CurrentTeam.Min_number))))));
                this.offset = Convert.ToInt32(Math.Ceiling(Convert.ToDouble((double)(5.0 * (Convert.ToDouble(this.CurrentTeam.Now_number) / Convert.ToDouble(this.CurrentTeam.Min_number))))));
            }
            this.blockflv1.UpdateView(this.CurrentTeam);
            this.blockask1.UpdateView(this.CurrentTeam, true);
            this.blockshare1.UpdateView(this.CurrentTeam);
            this.UpdateView(this.CurrentTeam.Id);
        }
    }

    public string type(Team model)
    {
        string str = "";
        if (model.Team_type == "seconds")
        {
            str = "今日秒杀";
        }
        if (model.Team_type == "goods")
        {
            str = "今日热销";
        }
        if (model.Team_type == "normal")
        {
            str = "今日团购";
        }
        return str;
    }

    public void UpdateView(int teamid)
    {
        int sideteam = 0;
        if (base.ASSystem != null)
        {
            sideteam = base.ASSystem.Sideteam;
            if (sideteam != 0)
            {
                string str = " 1=1  ";
                if (base.CurrentCity != null)
                {
                    str = string.Concat(new object[] { str, " and (City_id=", base.CurrentCity.Id, " or city_id=0)" });
                }
                str = string.Concat(new object[] { str, " and Team_type='normal' and id<>", teamid, " and Begin_time<='", DateTime.Now.ToString(), "' and end_time>='", DateTime.Now.ToString(), "'" });
                DataTable dt = DbHelperSQL.SelectByFilter("*", "", "", string.Concat(new object[] { "(select top ", sideteam, " * from team where ", str, " order by  sort_order desc,Begin_time desc,id desc ) as t1" }));
                this.otherteams = new Maticsoft.BLL.Team().DataTableToList(dt);
                if (base.CurrentTeam != null)
                {
                    this.blockask1.UpdateView(base.CurrentTeam, false);
                }
            }
        }
    }

}